import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class Main {
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        int hex = 0;

        hex = 0x98;

        System.out.println("hex = " + hex);

        FutureTask<String> task = new FutureTask<String>(()->{
            System.out.println("223");
            return "147";
        });

        AtomicInteger atomicInteger = new AtomicInteger(5);
        ExecutorService executor = Executors.newFixedThreadPool(5);
        CompletableFuture<String> completedFuture = CompletableFuture.supplyAsync(()->{
            // 模拟耗时操作
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < 100; i++) {
                atomicInteger.getAndIncrement();
            }
            return "Completed Future";
        });

        System.out.println("completedFuture.get() = " + completedFuture.get(3100,TimeUnit.MILLISECONDS));
        System.out.println("atomicInteger = " + atomicInteger);
        List<Callable<Integer>> tasks = new ArrayList<>();

        long startTime = System.currentTimeMillis();

        for (int i = 1; i <= 5; i++) {
            final int taskId = i;
            tasks.add(() -> {
                Thread.sleep(1000); // 模拟长时间计算
                System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
                return taskId * 10; // 返回计算结果
            });

        }

        try {
            // 提交所有任务并获取结果
            List<Future<Integer>> futures = executor.invokeAll(tasks);
            for (Future<Integer> future : futures) {
                System.out.println("Result: " + future.get());
            }
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        } finally {
            executor.shutdown();
        }

        long endTime = System.currentTimeMillis();
        long duration = endTime - startTime;
        System.out.println("耗时: " + duration + "毫秒");
    }
}

class CallableClass implements Callable<String>, Runnable{

    @Override
    public String call() throws Exception {

        return null;
    }

    @Override
    public void run() {

    }
}
